package hn.cch.init;

import hn.cch.cache.UserCache;
import hn.cch.constant.Path;
import hn.cch.model.User;
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InitUser {
    private static Logger logger = LoggerFactory.getLogger(InitUser.class);


    /**
     * 读取YAML格式配置文件初始化用户信息
     */
    public static void initUser(File file){
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(file);
            Element root = document.getRootElement();
            List<Element> elements = root.elements();

            for (Element element:elements) {
                User user = new User();
                user.setUserId(element.elementText("userId"));
                user.setUserKey(element.elementText("userKey"));
                user.setUserName(element.elementText("userName"));
                user.setUserType(element.elementText("userType"));
                user.setUserStatus(element.elementText("userStatus"));
                user.setUserUrl(element.elementText("userUrl"));

                logger.info("user cache : " + user.getUserId());
                UserCache.setKeyValue(user.getUserId(), user);
            }
            


        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        initUser(new File(Path.USER_PATH));
        String userId = "00000";
        User user = (User) UserCache.getValue(userId);
        logger.info("userId = " + userId + " -> user = " + user);

    }
}
